package org.example;

import java.util.HashMap;
import java.util.Map;
//解释器模式
//定义空调命令解析器
public class AirConditionerCommandInterpreter {
    private Map<String, AirConditionerCommand> commands;

    public AirConditionerCommandInterpreter(AirConditioner airConditioner) {
        commands = new HashMap<>();
        commands.put("set_temp", new SetTemperatureCommand(airConditioner, 0));
    }

    public AirConditionerCommand interpret(String input) {
        String[] tokens = input.split(" ");
        AirConditionerCommand command = commands.get(tokens[0]);
        if (command != null && tokens.length == 2) {
            ((SetTemperatureCommand) command).temperature = Integer.parseInt(tokens[1]);
            return command;
        }
        return null;
    }
}